package xin.marcher.module.label.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.label.api.LabelApi;
import xin.marcher.module.label.domain.dto.LabelDTO;
import xin.marcher.module.label.domain.dto.LabelResultDTO;
import xin.marcher.module.label.domain.request.LabelRequest;
import xin.marcher.module.label.domain.request.QueryLabelRequest;
import xin.marcher.module.label.service.LabelService;

/**
 * 标签操作API
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = LabelApi.class, retries = 0)
public class LabelApiImpl implements LabelApi {

    @Autowired
    private LabelService labelService;

    /**
     * 添加标签接口
     *
     * @param request
     */
    @Override
    public JsonResult<LabelResultDTO> saveLabel(LabelRequest request) {
        try {
            LabelResultDTO result = labelService.saveLabel(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据条件分页查询标签列表接口
     *
     * @param request
     */
    @Override
    public JsonResult<PageResult<LabelDTO>> queryLabelPage(QueryLabelRequest request) {
        try {
            PageResult<LabelDTO> result = labelService.queryLabelPage(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
